(function ($) {
    if (window.R) {
        return;
    }

window.R = {
    zh_CN: {
        alert : {
            title: {
                error : '错误', warn: '警告', prompt: '提问', info: '信息', success: '成功'
            }, 
            button: {
                ok: '确定', cancel: '取消', yes: '是', no: '否', save: '保存', rollBack: '回滚'
            }
        },
        index : {
            labels: {
                'btn-cloud-server':'服务器配置管理',
                'btn-create': '新建隧道',
                'btn-edit': '编辑隧道',
                'btn-remove': '删除隧道',
                'btn-connected': '连接',
                'btn-disconnected': '断开',
                'btn-settings': '系统设置',
                'btn-help': '帮助',
                'menu-help': '帮助',
                'menu-about': '关于...',
                'btn-about':'关于...'
            },
            messages: {
                'confirm-delete':'您确定要删除隧道[<strong>${name}</strong>吗？]',
            }
        },
        'server-manager': {
            labels: {
                'lbl-server-host':'服务器地址', 
                'lbl-manage-port':'隧道管理端口',
                'lbl-connector-port':'隧道连接端口',
                'btn-create':'新建服务器配置',
                'btn-remove':'删除服务器配置',
                'btn-ok':'保存',
                'btn-cancel':'取消'
            },
            messages: {
                'prompt-cancel':'您还有未保存的数据，是否确定取消编辑？',
                'confirm-save':'您还有数据未保存！<br/>您要先保存吗?<br/><br/><strong>如果您选择了"取消"按钮，未保存的数据将丢失!!!</strong>',
                'error-name-exists': '已经存在名为 [<strong>${name}</strong>] 的服务器配置!',
                'confirm-delete':'您确定要删除服务器配置[<strong>${name}</strong>吗？]',
                'server-in-use':'服务器配置[<strong>${name}</strong>]正在使用，不能删除',
                'confirm-close':'您还有数据未保存，确定要关闭吗？',
                'confirm-lose-data':'您还有数据未保存，若选择 [<strong><i>确定</i></strong>] 将导致未保存的数据丢失。<br/>您确定要继续吗？',
                'unknown-error':'未知错误',
                'save-ok':'保存成功',
                'save-fail':'保存失败',
                'delete-ok':'删除成功',
                'delete-fail':'删除失败'
            }
        },
        tunnel : {
            labels: {
                'btn-remove': '删除隧道',
                'btn-connected': '连接',
                'btn-disconnected': '断开',
                'btn-ok':'保存',
                'btn-cancel':'取消',

                'lbl-tunnel-name':'隧道名称',
                'lbl-server':'服务器配置',
                'lbl-mapping-port':'云端映射端口',
                'lbl-local-server':'本地服务器地址',
                'lbl-local-service':'本地服务端口',
                'lbl-server-selector':'-- 请选择服务器 --',
                'lbl-auto-connect':'自动重连'
            },
            messages: {
                'confirm-delete':'您确定要删除隧道 [<strong>${name}</strong>] 吗？'
            }
        },
        settings : {
            labels : {
                
            }
        }
    },
    en_US: {
        alert : {
            title: {
                error : 'Error', warn: 'Warning', prompt: 'Question', info: 'Information', success: 'Success'
            }, 
            button: {
                ok: 'OK', cancel: 'Cancel', yes: 'Yes', no: 'No', save: 'Save', rollBack: 'Roll Back'
            }
        },
        labels: {
            'btn-cloud-server':'Server Management',
            'btn-create': 'New Tunnel',
            'btn-edit': 'Edit Tunnel',
            'btn-remove': 'Remove Tunnel',
            'btn-connected': 'Active Tunnel',
            'btn-disconnected': 'Disable Tunnel',
            'btn-settings': 'System Settings',
            'btn-help': 'Help',
            'menu-help': 'Help ...',
            'menu-about': 'About ...',
            'btn-about': 'About ...'
        },
        'server-manager': {
            labels: {
                'lbl-server-host':'Host', 
                'lbl-manage-port':'Manage Port',
                'lbl-connector-port':'Connector Port',
                'btn-create':'New Server Configuration',
                'btn-remove':'Remove Server Configuration',
                'btn-ok':'Save',
                'btn-cancel':'Cancel'
            },
            messages: {
                'prompt-cancel':'You have unsaved data. <br/>Are you sure you want to cancel editing?',
                'confirm-save':'You have unsaved data.<br/>Do you want to save it before creating a new one?' + 
                               '<br/><br/><strong>If "Cancel" button clicked, the unsaved data will be lost!!!</strong>',
                'error-name-exists': 'The server named [<strong>${name}</strong>] already exists!',
                'confirm-delete':'Are you sure to delete the server configuration [<strong>${name}</strong>?]',
                'server-in-use':'Could not delete server configuration [<strong>${name}</strong>] because it is in use',
                'confirm-close':'There are unsaved data, are you sure to close window?',
                'confirm-lose-data':'You still have unsaved data. If you select [<strong><i>OK</i></strong>], the unsaved data will be lost. <br/> Are you sure you want to continue?',
                'unknown-error':'Unknown Error',
                'save-ok':'Save successed',
                'save-fail':'Save failed',
                'delete-ok':'Delete successed',
                'delete-fail':'Delete failed'
            }
        },
        tunnel : {
            labels: {
                'btn-remove': 'Remove Tunnel',
                'btn-connected': 'Active Tunnel',
                'btn-disconnected': 'Disable Tunnel',
                'btn-ok':'Save',
                'btn-cancel':'Cancel',

                'lbl-tunnel-name':'Tunnel Name',
                'lbl-server':'Server Configuration',
                'lbl-mapping-port':'Remote Mapping Port',
                'lbl-local-server':'Local Server Address',
                'lbl-local-service':'Local Service Port',
                'lbl-server-selector':'-- Pickup a Server --',
                'lbl-auto-connect':'Auto Reconnection'
            },
            messages: {
                'confirm-delete':'Are you sure to remove tunnel [<strong>${name}</strong>] ?'
            }
        }
    }
};

    var reg = /\$\{(.*?)\}/g;
    window.RS = function (res, key, labels, locale, nullable) {
        if (!res) {
            return key === undefined || key === null ? '' : key;
        }

        if (key === undefined || key === null || key === '') {
            return '';
        }

        labels = labels || {};
        locale = locale || window.LOCALE || 'en_US';
        var a = key.split ('.'), c = res [locale];
        for (var i = 0; i < a.length; i ++) {
            if (!(a[i] in c)) {
                return nullable ? '' : key;
            }
            c = c[a[i]];
        }

        c = (c === undefined || c === null ? '' : c);
        if (c && c.indexOf ('${') >= 0) {
            c = c.replace (reg, function (pattern, key) {
                if (key in labels) {
                    return labels [key];
                }

                var value = window.RS (res, key, {}, locale, true);
                if (value !== '') {
                    return value;
                }

                return pattern;
            });
        }
        return c;
    }

    // window.RS = getResource;
})(jQuery);